package com.sjb.sixteen_journey_backend.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.sjb.sixteen_journey_backend.common.R;
import com.sjb.sixteen_journey_backend.entity.Comment;
import com.sjb.sixteen_journey_backend.entity.Message;
import com.sjb.sixteen_journey_backend.entity.User;
import com.sjb.sixteen_journey_backend.entity.vo.UserBaseMsgVo;
import com.sjb.sixteen_journey_backend.mapper.MessageMapper;
import com.sjb.sixteen_journey_backend.service.IMessageService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sjb.sixteen_journey_backend.service.IUserService;
import com.sjb.sixteen_journey_backend.utils.ThreadLocalUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 评论信息列表 服务实现类
 * </p>
 *
 * @author jcz
 * @since 2024-07-23
 */
@Service
public class MessageServiceImpl extends ServiceImpl<MessageMapper, Message> implements IMessageService {

    @Autowired
    private IUserService userService;

    @Override
    public R getMessages() {
        try {
            Map<String, Object> map = ThreadLocalUtil.get();
            Integer userId = (Integer) map.get("id");
            QueryWrapper<Message> wrapper = Wrappers.query();
            wrapper.eq("user_id", userId);
            wrapper.orderByDesc("postdate");
            List<Message> list = this.list(wrapper);
            for (Message comment : list) {
                User user = userService.getById(comment.getUserId());
                UserBaseMsgVo userBaseMsgVo = new UserBaseMsgVo();
                BeanUtils.copyProperties(user, userBaseMsgVo);
                comment.setUser(userBaseMsgVo);
                if (comment.getTargetId() != null) {
                    user = userService.getById(comment.getTargetId());
                    userBaseMsgVo = new UserBaseMsgVo();
                    BeanUtils.copyProperties(user, userBaseMsgVo);
                    comment.setTargetUser(userBaseMsgVo);
                }
            }
            return R.success(list, "已获取用户有关的评论消息！");
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public R getMessageById(Integer id) {
        try {
            Message m = this.getById(id);
            if (m != null) {
                if (!m.getIsLooked()) {
                    m.setIsLooked(true);
                    this.updateById(m);
                }
                return R.success(m, "已获取该消息！");
            }
            return R.error("该消息不存在！");
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public R deleteCommentMessage(Integer commentId) {
        try {
            if (this.removeById(commentId)) {
                return R.success(null, "删除成功");
            }
            return R.error("删除失败");
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
